package com.htfyun.factorytest.common;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.SystemProperties;

import com.htfyun.factorytest.RootApplication;
import com.htfyun.factorytest.utils.DisplayUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class StandbyUtil {
    public static String srcPath = "/oem/media/standby.png";

    public static void setStandbyPng(String s) {
        String dstPath = FTUtils.getFTStorageDirectory() + File.separator + "standby_aging_done.png";
        int screenWidth = DisplayUtil.getScreenWidth(RootApplication.getContext());
        int screenHeight = DisplayUtil.getScreenHeight(RootApplication.getContext());

        Paint paint = new Paint();
        paint.setColor(Color.LTGRAY);
        paint.setFakeBoldText(true);
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setTypeface(Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD));
        paint.setTextSize(96);
        try {
            Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
            int densityDpi = RootApplication.getContext().getResources().getDisplayMetrics().densityDpi;
            bitmap.setDensity(densityDpi);
            bitmap.eraseColor(Color.WHITE);

            Canvas canvas = new Canvas(bitmap);
            canvas.drawText(s, screenWidth * 0.25f, screenHeight * 0.5f, paint);

            File f = new File(dstPath);
            FileOutputStream fos = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        SystemProperties.set(Constants.STANDBY_PNG, dstPath);
    }

    public static void resetStandbyPng() {
        SystemProperties.set(Constants.STANDBY_PNG, srcPath);
    }
}
